package Demo7;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyThread extends Thread{
    private static int ticket = 0;
    //static Object obj = new Object();// 锁对象要是唯一的如果是this那就不是唯一的
    static Lock lock = new ReentrantLock();
    @Override
    public void run() {
        // 卖100张票
        while (true) {
           //synchronized (MyThread.class) {
            lock.lock();
            try {
                if (ticket < 100) {
                    Thread.sleep(100);
                    ticket++;
                    System.out.println(getName() +"正在卖第" + ticket + "张票");
                } else {
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
           //}

        }
    }
}
